import facade
from xjlibrary.mprocesspoll.MThreadingRun import MThreadingRun
from xjlibrary.mrequest import baserequest
from xjlibrary.myredis.myredisclient import getDataFromRedis
from xjlibrary.our_file_dir import BaseDir, ImageFile

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "sipopatent", "download", "cover")
configfile = BaseDir.get_new_path(curPath, "db.ini")

logger = facade.get_streamlogger()

keytransformdicts = {"port": "db_port", "host": "db_host", "passwd": "db_pw", "user": "db_user",
                     "db": "db_name",
                     "chartset": "db_charset"}

mysqlutils = facade.MysqlUtiles(configfile, "db", logger=logger, keytransformdicts=keytransformdicts)


def SelectDB():
    sql = "select pub_no,coverurl from cover_pub where stat=0 limit 1000"
    rows = mysqlutils.SelectFromDB(sql)
    return rows


def readDBProxy():
    return getDataFromRedis(curPath, 'db.ini')


def get_url(url, listproxy, endstring=""):
    BoolResult, errString, r = baserequest.MProxyRequest(url, endstring=endstring, proxyRingList=listproxy,
                                                         timeout=(30, 60))
    if not BoolResult:
        return None
    return r


def downandsavecover(cover, filepath, listproxy):
    r = get_url(cover, listproxy)
    if r:
        result_bool = ImageFile.save_img_to_jpg(r.content, filepath)
        return result_bool
    else:
        return False


def down_cover(result_queue, listproxy, pub_no, coverurl):
    if coverurl == "images/cp_noimg.jpg" or coverurl == '':
        sql = "update cover_pub set coveruri='',stat=1 where pub_no='{}'".format(pub_no)
        result_queue.put(sql)
    else:
        pub_no_lower = pub_no.lower()
        dirpath = BaseDir.get_new_path(sPath, pub_no_lower[:3], pub_no_lower[3:7])
        if not BaseDir.is_dir_exists(dirpath):
            BaseDir.create_dir(dirpath)
        filepath = BaseDir.get_new_path(dirpath, pub_no_lower + ".jpg")
        if not BaseDir.is_file_exists(filepath):
            url = "http://epub.sipo.gov.cn/" + coverurl
            result_bool = downandsavecover(url, filepath, listproxy)
            if result_bool:
                sqluristr = "/smartlib/cnipapatent/" + pub_no_lower[:3] + "/" + pub_no_lower[3:7] + "/" + str(
                    pub_no_lower) + ".jpg"
                sql = "update cover_pub set coveruri='{}',stat=1 where pub_no='{}'".format(sqluristr, pub_no)
                result_queue.put(sql)
            else:
                sql = "update cover_pub set coveruri='',stat=0,failcount = failcount+1 where pub_no='{}'".format(pub_no)
                result_queue.put(sql)
        else:
            sqluristr = "/smartlib/cnipapatent/" + pub_no_lower[:3] + "/" + pub_no_lower[3:7] + "/" + str(
                pub_no_lower) + ".jpg"
            sql = "update cover_pub set coveruri='{}',stat=1 where pub_no='{}'".format(sqluristr, pub_no)
            result_queue.put(sql)


def InsertIntoDbFromList(listSql):
    mysqlutils.ExeSqlListToDB(listSql, errExit=True)


class DownPageThreadRun(MThreadingRun):
    def __init__(self, num, func):
        super(DownPageThreadRun, self).__init__(num, func)

    def setTask(self, *args, **kwargs):
        rows = SelectDB()
        listid = []
        for pub_no, coverurl in rows:
            listid.append(pub_no)
        if len(listid) == 1:
            listid.append("test")
        if len(listid) > 0:
            sql = "update cover_pub set stat=-1 where  `pub_no` in {}".format(tuple(listid))
            mysqlutils.ExeSqlToDB(sql, errExit=True)
        for pub_no, coverurl in rows:
            # 将每一页加入任务队列
            self.add_job(self.func, self.list_proxy, pub_no, coverurl)

    def dealresult(self, *args, **kwargs):
        InsertIntoDbFromList(self.results)

    def setProxy(self, proxysList=None):
        rows = readDBProxy()
        MThreadingRun.setProxy(self, list(rows))


if __name__ == "__main__":
    func = down_cover
    threadrun = DownPageThreadRun(50, func)
    threadrun.run()
